﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace PrinteMaster
{
    public partial class 充值窗口 : Form
    {

        private string m_lpstrId="",m_lpstrIdHit,m_lpstrMoneyHit;
        private int m_intMoney=0;
        private MySqlHelper.ManagerInfo m_ManagerInfo;
        public 充值窗口()
        {
            InitializeComponent();
        }

        private void 充值窗口_Load(object sender, EventArgs e)
        {
            m_lpstrIdHit = textBox充值账号.Text;
            m_lpstrMoneyHit = textBox充值金额.Text;
        }
        /// <summary>
        /// 静态创建充值窗口
        /// </summary>
        /// <param name="lpstrName"></param>
        /// <returns></returns>
        public static 充值窗口 create(MySqlHelper.ManagerInfo info,客服窗口 window)
        {
            充值窗口 c = new 充值窗口();
            c.m_ManagerInfo = info;
            c.ShowDialog(window);
            return c;
        }

        private void textBox充值金额_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsNumber(e.KeyChar) && e.KeyChar != (char)8)
            {
                e.Handled = true;
            }
            
        }
        private void button充值_Click(object sender, EventArgs e)
        {
            if (!textBox充值账号.Text.Equals("") && !textBox充值账号.Text.Equals(m_lpstrIdHit))
                m_lpstrId = textBox充值账号.Text;

            // 判断充值金额输入框是否处于已输入状态;
            if (!textBox充值金额.Text.Equals("") && !textBox充值金额.Text.Equals(m_lpstrMoneyHit))
                m_intMoney = Convert.ToInt32(textBox充值金额.Text);

            // 判断是否为负数
            if (Convert.ToInt32(textBox充值金额.Text) <= 0)
            {
                MessageBox.Show("充值金额必须大于0,需扣钱请联系管理员到总端操作!", "提示");
                return;
            }

            // 检查参数
            if (m_lpstrId.Equals(""))
            {
                // 
                MessageBox.Show("请输入账号");
                return;
            }
            if (m_intMoney == 0)
            {
                //
                MessageBox.Show("请输入充值金额");
                return;
            }

            // 如果账号和金额都正确的被输入,提示确认充值
            ////////////////// 检测参数是否准确 //////////////
            string userName, userNumber,userPhoneNumber;
            if (DataBase.getInstance().isUserId(m_lpstrId, out userNumber, out userName, out userPhoneNumber) != DataBase.Error.ok)
            {
                // 如果 会员简表内没有这个记录
                MessageBox.Show("没有该会员!", "提示");
                return;
            }

            ////////////////// 提示充值者信息  //////////////
            string stringbuild = "\n 充值账号:" + m_lpstrId + "   |   充值金额:" + m_intMoney.ToString() + ".00 元\n充值者姓名:" + userName;
            if (MessageBox.Show("确认充值吗？" + stringbuild, "人工充值", MessageBoxButtons.OKCancel) == DialogResult.OK)
            {
                // 进入充值
                // 填充结构体
                DataBase.PayInfo info = new DataBase.PayInfo();
                info.managerId = m_ManagerInfo.id;
                info.managerName = m_ManagerInfo.name;
                info.money = m_intMoney;
                info.time = DateTime.Now;
                info.userId = m_lpstrId;
                info.userName = userName;
                info.userNumber = userNumber;
                info.userPhoneNumber = userPhoneNumber;
                DataBase.Error error = DataBase.getInstance().addPayInfo(info);//建档
                error = DataBase.getInstance().addUserMoney(m_lpstrId, (double)m_intMoney);
                if (error == DataBase.Error.ok)
                {
                    MessageBox.Show("充值成功!");
                    this.Close();
                }
                else
                {
                    MessageBox.Show("充值失败!");
                    return;
                }
                string msg = string.Format("为客户[{0}]充值{1}.00元", userName, m_intMoney.ToString());
                Log.managerLog(msg, m_ManagerInfo.id, m_ManagerInfo.name, m_ManagerInfo.user);
                return;
            }


        }

        private void textBox充值账号_Leave(object sender, EventArgs e)
        {
            textBox充值账号_Enter(null, null);
        }

        private void textBox充值账号_Enter(object sender, EventArgs e)
        {
            
            // 如果账号为提示信息，则变为空
            if (textBox充值账号.Text.Equals(m_lpstrIdHit))
            {
                textBox充值账号.Text = "";
            }
            // 如果为空，则变为提示信息
            else if (textBox充值账号.Text.Equals(""))
            {
                textBox充值账号.Text = m_lpstrIdHit;
            }
        }

        private void textBox充值金额_Enter(object sender, EventArgs e)
        {
            if (textBox充值金额.Text.Equals(m_lpstrMoneyHit))
            {
                textBox充值金额.Text = "";
            }
            // 如果为空，则变为提示信息
            else if (textBox充值金额.Text.Equals(""))
            {
                textBox充值金额.Text = m_lpstrMoneyHit;
            }
        }

        private void textBox充值金额_Leave(object sender, EventArgs e)
        {
            textBox充值金额_Enter(null, null);
        }

        private void textBox充值账号_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsNumber(e.KeyChar) && e.KeyChar != (char)8)
            {
                e.Handled = true;
            }
        }

        private void textBox充值金额_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                button充值_Click(null, null);
            }
        }

        private void textBox充值金额_TextChanged(object sender, EventArgs e)
        {

        }

        private void textBox充值账号_TextChanged(object sender, EventArgs e)
        {

        }
    }
}
